package com.wb.creational.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 魏彪
 */
public class LoadBalancer {

    private static LoadBalancer instance = null;

    private List<String> serverList;

    public LoadBalancer() {
        serverList = new ArrayList<>();
    }

    public static LoadBalancer getInstance() {
        if (instance == null) {
            instance = new LoadBalancer();
        }

        return instance;
    }

    public void addServer(String server) {
        serverList.add(server);
    }

    public void removeServer(String server) {
        serverList.remove(server);
    }

    public String getServer() {
        Random random = new Random();
        return serverList.get(random.nextInt(serverList.size()));
    }
}
